<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * 学员管理
 */
class Student extends MY_Controller {
public $left_menu_min;

  function __construct(){
    parent::__construct();

      $parent_id = 7;
      //获取school二级菜单
      $this->load->model('School_manage_menu_model');
      $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
      // print_r($menu_list);exit;
      $this->smarty->assign("menu_list", $menu_list);

      $this->left_menu_min = array("menu" => "会员管理", "curr" => '/student', "list" => $menu_list);
      $this->smarty->assign("left_menu_min", $this->left_menu_min);

      $this->smarty->assign("menu", 'users');

  }

  /**
	 * 获取学生列表
	 * @return [type] [description]
	 */
	function index($name = 0, $school_campus_id = 0, $st = 0, $et = 0, $mobile = 0, $limit = 0){
        $post_data = $this->input->post();
        if($post_data){
            $name = !empty($post_data['name']) ? $post_data['name'] : 0;
            $school_campus_id = !empty($post_data['school_campus_id']) ? $post_data['school_campus_id'] : 0;
            $st = !empty($post_data['st']) ? $post_data['st'] : 0;
            $et = !empty($post_data['et']) ? $post_data['et'] : 0;
            $mobile = !empty($post_data['mobile']) ? $post_data['mobile'] : 0;
        }

        $conditions['school_id'] = $this->user_data['school_id'];
        $conditions['name'] = $name;
        $conditions['school_campus_id'] = $school_campus_id;
        $conditions['st'] = $st;
        $conditions['et'] = $et;
        $conditions['mobile'] = $mobile;
    		$this->load->model('School_student_model');
    		$student = $this->School_student_model->get_student_by_school($limit, $this->offset, $conditions);

        $this->load->library('pagination');
        $page_config['base_url'] = '/student/index/' . $name . '/' . $school_campus_id . '/' . $st . '/' . $et . '/' . $mobile;
        $page_config['total_rows'] = $student['count'];
        $page_config['per_page'] = $this->offset;
        $page_config['uri_segment'] = 7;
        $this->pagination->initialize($page_config);
        $pagination = $this->pagination->create_links();
        $this->smarty->assign("pagination", $pagination);


		$this->smarty->assign("student", $student['result']);
        $this->load->model('Schoolo2o_model');
        $campus_list = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
        $this->smarty->assign("campus_list", $campus_list);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->display("student/student.html");
	}

  /**
   * 所选科目
   * @param  [type] $student_id [description]
   * @return [type]             [description]
   */
  public function subject_list($student_id)
  {
    //学员详情
    $this->load->model('Student_model');
    $student = $this->Student_model->get_student_by_id($student_id);
    $this->smarty->assign("student", $student);
    // print_r(json_encode($student['class_time']));exit;
    //获取学员所选科目
    $this->load->model('Student_Course_model');
    $course_list = $this->Student_Course_model->get_student_course_list_by_school($this->user_data['school_id'],$student_id);
    $this->smarty->assign("course_list", $course_list);
    $this->smarty->display("student/student_subject_list.html");
  }

  /**
   * 添加线下学员
   * @param string $value [description]
   */
  public function add($value='')
  {
    $post_data = $this->input->post();

    if($post_data){

      $this->load->model('Student_model');
      $student_id = $this->Student_model->add_student_offline($post_data,$this->user_data['school_id']);
      if($student_id>0){
        header("Location:/student/add_subject/{$student_id}");exit;
      } else{
        header("Location:/student");exit;
      }
    }
    //获取校区
    $this->load->model('Schoolo2o_model');
    $campus = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
    $this->smarty->assign("campus", $campus);

    $this->smarty->display("student/student_add.html");
  }

  /**
   * 删除科目
   * @param [type] $subject_id [description]
   */
  function delete_subject($student_course_id,$student_id){
    $this->db->delete('o2o_student_course',array('id' => $student_course_id ));
    header("Location:/student/subject_list/{$student_id}");exit;
  }

  /**
   * 添加学员面授课的科目
   * @param [type] $student_id [description]
   */
  public function add_subject($student_id)
  {
    $this->smarty->assign("student_id", $student_id);

    $post_data = $this->input->post();
    if($post_data){


      $this->load->model('School_student_model');
      $result = $this->School_student_model->add_student_course($this->user_data['school_id'],$student_id,$post_data['course']);
      header("Location:/student/subject_list/{$student_id}");exit;
    }

    //获取线下课程分类、分类下的课程
    $this->load->model('Courseo2o_model');
    $cate_list = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
    for ($i=0; $i < count($cate_list); $i++) {
      $course_list = $this->Courseo2o_model->get_offline_course_by_cate($cate_list[$i]['id']);
      $cate_list[$i]['course_list']=$course_list;
    }
    $this->smarty->assign("cate_list", $cate_list);
    // print_r($cate_list);exit;
    $this->smarty->display("student/student_add_subject.html");
  }


	/**
	 * 待分班学员list
	 * @return [type] [description]
	 */
	function undistributed($name = 0, $school_campus_id = 0, $st = 0, $et = 0, $mobile = 0, $course_id = 0, $limit = 0){
        $post_data = $this->input->post();
        if($post_data){
            $name = !empty($post_data['name']) ? $post_data['name'] : 0;
            $school_campus_id = !empty($post_data['school_campus_id']) ? $post_data['school_campus_id'] : 0;
            $st = !empty($post_data['st']) ? $post_data['st'] : 0;
            $et = !empty($post_data['et']) ? $post_data['et'] : 0;
            $mobile = !empty($post_data['mobile']) ? $post_data['mobile'] : 0;
            $course_id = !empty($post_data['course_id']) ? $post_data['course_id'] : 0;
            
        }
        if(!empty($post_data['course_cate'])){
          $conditions['course_cate'] = $post_data['course_cate'];
        }

        $conditions['school_id'] = $this->user_data['school_id'];
        $conditions['name'] = $name;
        $conditions['school_campus_id'] = $school_campus_id;
        $conditions['st'] = $st;
        $conditions['et'] = $et;
        $conditions['mobile'] = $mobile;
        $conditions['course_id'] = $course_id;
		    $this->load->model('School_student_model');
		    $student = $this->School_student_model->get_undistributed_course_student($limit, $this->offset, $conditions);
		    $this->smarty->assign("student", $student);

        $this->load->library('pagination');
        $page_config['base_url'] = '/student/undistributed/' . $name . '/' . $school_campus_id . '/' . $st . '/' . $et;
        $page_config['total_rows'] = $student['count'];
        $page_config['per_page'] = $this->offset;
        $page_config['uri_segment'] = 9;
        $this->pagination->initialize($page_config);
        $pagination = $this->pagination->create_links();
        $this->smarty->assign("pagination", $pagination);
		    $this->smarty->assign("student", $student['result']);
        $this->load->model('Schoolo2o_model');
        $campus_list = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
        $this->smarty->assign("campus_list", $campus_list);

        //获取线下课程分类
        $this->load->model("Courseo2o_model");
        $course_cate = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
        $this->smarty->assign("course_cate", $course_cate);

        $this->smarty->assign("conditions", $conditions);
		    $this->smarty->display("student/student_undistributed.html");
	}

  /**
   * 为学员分班
   * @param  [type] $student_course_id [description]
   * @return [type]                    [description]
   */
  public function allot_class($student_course_id)
  {
    //获取学员报名的课程信息
    $this->load->model('Student_Course_model');
    $student_course_detail = $this->Student_Course_model->get_student_course_by_id($student_course_id);
    $this->smarty->assign("student_course_detail", $student_course_detail);

    $this->load->model('Classo2o_model');
    //判断状态
    if($student_course_detail['status']==1){
      //已分班
      $class_list = $this->Classo2o_model->get_class_by_student($student_course_detail['student_id'],$student_course_detail['school_id'],$student_course_detail['course_id']);
      $this->smarty->assign("class_list", $class_list);
    }else{
      //获取对应的班级

      $class_list = $this->Classo2o_model->get_class_by_school_course($student_course_id);
      $this->smarty->assign("class_list", $class_list);
    }


		$this->smarty->display("student/allot_class.html");
  }

  /**
   * 为学员分班
   * @param  [type] $student_course_id [description]
   * @return [type]                    [description]
   */
  public function allot_class_to($student_course_id)
  {
    $post_data = $this->input->post();
    if($post_data){

			$this->load->model('Student_Course_model');
			$result = $this->Student_Course_model->allot_class_to($student_course_id,$post_data['class_id']);
      // print_r($result);exit;
			if($result>0){
				print_r('{"result":"0","msg":"分班成功"}');exit;
	    }
		}
  }

  function update_score(){
    $post_data = $this->input->post();
    if(empty($post_data['student_id'])){
      $result['error'] = 1;
      $result['msg'] = '请选择一个学生';
      echo json_encode($result);exit;
    }
    if(empty($post_data['score'])){
      $result['error'] = 1;
      $result['msg'] = '请填写分数';
      echo json_encode($result);exit;
    }
    $this->db->where("student_id", $post_data['student_id']);
    $this->db->update("o2o_school_student", array("score" => $post_data['score']));
    $result['error'] = 0;
    $result['msg'] = '修改成功';

    $score_data['teacher_id'] = $this->user_data['teacher_id'];
    $score_data['student_id'] = $post_data['student_id'];
    $score_data['score'] = $post_data['score'];
    $score_data['content'] = $post_data['content'];
    $score_data['add_time'] = time();
    $this->db->insert("o2o_school_student_scores", $score_data);
    echo json_encode($result);exit;
  }

  function change_pwd($uid)
  {
    $this->smarty->assign("uid", $uid);

    $post_data = $this->input->post();
    if($post_data){
        $has_error = 0;

        if(empty($post_data['password'])){
          $this->smarty->assign("password_err", "请填写新密码");
          $has_error = 1;
        }
        if(empty($post_data['password2'])){
          $this->smarty->assign("password2_err", "确认新密码");
          $has_error = 1;
        }
        if($has_error==0){
          if($post_data['password']!=$post_data['password2']){
            $this->smarty->assign("password_err", "二次密码不一致");
          }
        }



      if($has_error==0){

        $md5_pass = md5($post_data['password']);
        $rand_str = '';
        for($i=0; $i<10; $i++){
          $rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
        }

        $password_str = md5($md5_pass.$rand_str);
        //更新user表
       $user_new = array(
         'rand_str' => $rand_str,
          'password' => $password_str,
       );

       $this->db->update('user',$user_new, array('id' => $uid));
        header("Location:/student");exit;
      }
    }
    $this->smarty->display("student/change_pwd.html");
  }
}
